<?php

/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2010 osCommerce

  Released under the GNU General Public License
 */

class bm_categories_cya {

    var $code = 'bm_categories_cya';
    var $group = 'boxes';
    var $title;
    var $description;
    var $sort_order;
    var $enabled = false;
    var $manufactures;
    var $manu_id;
    var $menu;
    var $flag_div = false;
    var $flag_one = false;
    var $flag_level_1 = false;
    var $jquery_css = '';

    function bm_categories_cya() {
        $this->title = 'Categories (cya)';
        $this->description = MODULE_BOXES_CATEGORIES_DESCRIPTION;

        if (defined('MODULE_BOXES_CATEGORIES_CYA')) {
            $this->sort_order = MODULE_BOXES_CATEGORIES_SORT_ORDER_CYA;
            $this->enabled = (MODULE_BOXES_CATEGORIES_CYA == 'True');

            $this->group = ((MODULE_BOXES_CATEGORIES_CONTENT_PLACEMENT_CYA == 'Left Column') ? 'boxes_column_left' : 'boxes_column_right');
        }
    }

    /**
     * function check sun cat
     * @author Nguyen Manh Hung
     * @param int $id
     * @return int
     */
    function check_sub_cat($id) {

        $check_query = tep_db_query("select count(c.categories_id) as count_row from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '" . (int) $id . "'");
        $value = tep_db_fetch_array($check_query);
        if ($value['count_row']) {
            return 1;
        } else {
            return 0;
        }
    }

    function get_menu() {
        global $cPath_array;
        $id = $cPath_array[count($cPath_array) - 1];
        //$subcategories_array = array();
        //$subcats = tep_get_subcategories($subcategories_array, $id);
        //$sql = 'SELECT DISTINCT p.manufacturers_id from products as p INNER JOIN products_description as pd on p.products_id = pd.products_id order by pd.products_viewed DESC';
       $sql = "select distinct m.manufacturers_id";
      
	   $sql .= " from " . TABLE_PRODUCTS . " p left join " . TABLE_MANUFACTURERS . " m using(manufacturers_id)";
       $sql .= " , " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_CATEGORIES . " c, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c";
       $sql .= " where p.products_status = '1' and p.products_id = pd.products_id and p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and c.categories_id=" . $id;
	//   $sql .= ORDER BY  `pd`.`products_viewed` ASC 
	//   echo $sql."<br>";
	   
       $exit_m = array();
       $m_id_query = mysql_query($sql);
      
	   while ($manu_id = tep_db_fetch_array($m_id_query)) {
            $exit_m[] = $manu_id['manufacturers_id'];
            $m_query = mysql_query("select manufacturers_name,manufacturers_id from " . TABLE_MANUFACTURERS . " where manufacturers_id = " . $manu_id['manufacturers_id'] . " order by manufacturers_id ASC");
            while ($manufacture = tep_db_fetch_array($m_query)) {
                $this->manufactures[] = $manufacture;
            }
        }

        /* $exit_id = implode(',', $exit_m);
          if (count($exit_m)) {
          $m_exit_query = mysql_query("select manufacturers_id, manufacturers_name from " . TABLE_MANUFACTURERS . " where manufacturers_id not in ($exit_id)");
          } else {
          $m_exit_query = mysql_query("select manufacturers_id, manufacturers_name from " . TABLE_MANUFACTURERS);
          }

          while ($m_exit_qu = tep_db_fetch_array($m_exit_query)) {
          $this->manufactures[] = $m_exit_qu;
          } */
        return $this->manufactures;
    }

    function cya_count_products_in_price($category_id, $price_from, $price_to) {
        global $currencies, $currency,$manu;
        $products_count = 0;

        $currencies_array = $currencies->currencies;
        $cCurrentcy = $currencies_array[$currency];
        $price_to = $price_to / $cCurrentcy['value'];
        $price_from = $price_from / $cCurrentcy['value'];
        
		if ($price_to ===9999999999)
		  {
			  $q= "select count(*) as total from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = p2c.products_id and p.products_status = '1' and p2c.categories_id = '" . (int) $category_id . "' and p.products_price >= '" . $price_from . "'";			  
		  }
		 else
		  {
			  $q="select count(*) as total from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = p2c.products_id and p.products_status = '1' and p2c.categories_id = '" . (int) $category_id . "' and p.products_price >= " . $price_from . " and p.products_price <= " . $price_to . "";
		  }
		 
		//echo $q."<br>";
		  
        $products_query = tep_db_query($q);      
		
        $products = tep_db_fetch_array($products_query);
        $products_count += $products['total'];
        if ($products_count) {
            return '(' . $products_count . ')';
        }
        return '';
    }

    function cya_count_products_in_manu($category_id, $manu_id) {
		
		global $where_price; //add by donghp - querry with price range
		
		$q = "select count(*) as total from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = p2c.products_id and p.products_status = '1' and p2c.categories_id = '" . (int) $category_id . "' and p.manufacturers_id ='" . $manu_id . "'".$where_price;
		
        $products_count = 0;
        
        $products_query = tep_db_query($q);

        $products = tep_db_fetch_array($products_query);
        $products_count += $products['total'];

        if ($products_count) {
            return '(' . $products_count . ')';
        }
        return '';
    }

    function show_currency($value_input) {
        
		global $currency, $currencies;

		//$currencies->format($order->info['shipping_cost'], true, $order->info['currency'], $order->info['currency_value']),
		//$currencies->format($num) add by donghp
		
        $currencies_array = $currencies->currencies;
        
		$cCurrentcy = $currencies_array[$currency]; //$cCurrentcy['decimal_places']
        $rate_filter = $cCurrentcy['rate_filter'];
        
		$value_out = $value_input * $rate_filter;
		
		$value_out = number_format($value_out,(int) $cCurrentcy['decimal_places']);
				
		/* Edit by donghp
        if($currency!='USD'){ //CLP rate_filter=0.50000000 | USD rate_filter=1
            $value_out = number_format($value_out,(int) $cCurrentcy['decimal_places']);
        }else{
            $value_out = number_format($value_out, 0);
        }
		*/
		//echo $rate_filter." = ".$value_out."<br>";
		
        return $value_out;//edit by donghp
    }

    function tep_show_category($counter) { //add by donghp
    global $tree, $categories_string, $cPath_array, $currency, $currencies,$current_category_id,$where_price;		
		
        $currencies_array = $currencies->currencies;
        
		$cCurrentcy = $currencies_array[$currency];
         
        $precision = $cCurrentcy ['decimal_places']; 
		
		//echo "precision=$precision <br>";
		//--------------------------------------------------------------------------
		$this->flag_one = false;
        
		$cPath_array_new = $cPath_array;
        
		$id = $cPath_array[count($cPath_array) - 1];				
		
        $parent = 0;
		
        if (count($cPath_array) - 2 >= 0) {
            $parent = $cPath_array[count($cPath_array) - 2];
        }
		
        $path_array = explode('_', $tree[$counter]['path']);
        $cid = $path_array[count($path_array) - 1];
        $cparent = $tree[$counter]['parent'];
		
        for ($i = 0; $i < $tree[$counter]['level']; $i++) {
            //$categories_string .= "&nbsp;&nbsp;";
        }

        if (is_array($cPath_array) && ( $tree[$counter]['level'] || in_array($counter, $cPath_array))) {
            if (!$tree[$counter]['level']) {
                //cho hien ra binh thuong
                $categories_string .= '<h3 id="h3_content_filter" class="ui-accordion-header ui-helper-reset ui-state-default ui-state-active ui-corner-top"><div><span class="ui-icon ui-icon-plus" style="float: left;"></span>';

                // start show name category
                $categories_string .= '<a href="';

                if ($tree[$counter]['parent'] == 0) {
                    $cPath_new = 'cPath=' . $counter;
                } else {
                    $cPath_new = 'cPath=' . $tree[$counter]['path'];
                }

                $categories_string .= tep_href_link(FILENAME_DEFAULT, $cPath_new) . '">';

                if (isset($cPath_array) && in_array($counter, $cPath_array)) {
                    $categories_string .= '<strong>';
                }

                // display category name
                $categories_string .= '<span class="block_title border_bottom" style="margin-left: 0px; width:110px;">' . $tree[$counter]['name'];

                $categories_string.='</span>';
                $categories_string .= '<div class="clear"></div>';
                //$categories_string .= $tree[$counter]['name'];

                if (isset($cPath_array) && in_array($counter, $cPath_array)) {
                    $categories_string .= '</strong>';
                }

                if (tep_has_category_subcategories($counter)) {
                    //$categories_string .= '-&gt;';
                }

                $categories_string .= '</a>';

                $categories_string .= '<span class="icon_back"><a href="' . tep_href_link(FILENAME_DEFAULT) . '">' . tep_image(DIR_WS_IMAGES . 'close-md-new.png', 'back') . '</a></span>';
                //end show name category

                $categories_string .= '</div></h3>';
                if (!$this->check_sub_cat($counter)) {
                    $categories_string.='<div id="div_content_filter" class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active">';
// onclick="javascript:any_price_click();"
                    $categories_string.='<div id="div_all_price" style="margin: 10px 0px 0px 10px; width:120px;">
                        <span style="color:#E17009; font-weight: bold;">Price</span>' .
                            '<div id="any_price" class="anyPrice" style="margin:5px 0px 0px 10px; ">
							<a href="#" onclick="javascript:any_price_click();"> &laquo; Any price</a></div>' .
                            '<div class="price" style="margin-left:0px;">';

                    if (SHOW_COUNTS == 'true') {
                        $categories_string .= '<div> <a href="javascript:void(0);" onclick="choose_price(0,' . $this->show_currency(40).','.$precision.');">D Under ' . $cCurrentcy['symbol_left'] . $this->show_currency(40) . $cCurrentcy['symbol_right'] . '</a>&nbsp;' . $this->cya_count_products_in_price($id, 0, $this->show_currency(40)) . '</div>' .
                                '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(40) . ',' . $this->show_currency(100) .','.$precision.');">' . $cCurrentcy['symbol_left'] . $this->show_currency(40) . $cCurrentcy['symbol_right'] . ' to ' . $cCurrentcy['symbol_left'] . $this->show_currency(100) . $cCurrentcy['symbol_right'] . '</a>&nbsp;' . $this->cya_count_products_in_price($id, $this->show_currency(40), $this->show_currency(100)) . '</div>' .
                                '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(100) . ',' . $this->show_currency(200) .','.$precision.');">' . $cCurrentcy['symbol_left'] . $this->show_currency(100) . $cCurrentcy['symbol_right'] . ' to ' . $cCurrentcy['symbol_left'] . $this->show_currency(200) . $cCurrentcy['symbol_right'] . '</a>&nbsp;' . $this->cya_count_products_in_price($id, $this->show_currency(100), $this->show_currency(200)) . '</div>' .
                                '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(200) . ',' . $this->show_currency(400) .','.$precision.');">' . $cCurrentcy['symbol_left'] . $this->show_currency(200) . $cCurrentcy['symbol_right'] . ' to ' . $cCurrentcy['symbol_left'] . $this->show_currency(400) . $cCurrentcy['symbol_right'] . '</a>&nbsp;' . $this->cya_count_products_in_price($id, $this->show_currency(200), $this->show_currency(400)) . '</div>' .
                                '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(400) . ',0,'.$precision.');">Over ' . $cCurrentcy['symbol_left'] . $this->show_currency(400) . $cCurrentcy['symbol_right'] . '</a>&nbsp;' . $this->cya_count_products_in_price($id, $this->show_currency(400), 9999999999) . '</div>';
                    } else {
                        $categories_string .= '<div> <a href="javascript:void(0);" onclick="choose_price(0,' . $this->show_currency(40).','.$precision.');">Under ' . $cCurrentcy['symbol_left'] . $this->show_currency(40) . $cCurrentcy['symbol_right'] . '</a></div>' .
                                '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(40) . ',' . $this->show_currency(100) .','.$precision.');">' . $cCurrentcy['symbol_left'] . $this->show_currency(40) . $cCurrentcy['symbol_right'] . ' to ' . $cCurrentcy['symbol_left'] . $this->show_currency(100) . $cCurrentcy['symbol_right'] . '</a></div>' .
                                '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(100) . ',' . $this->show_currency(200) .','.$precision.');">' . $cCurrentcy['symbol_left'] . $this->show_currency(100) . $cCurrentcy['symbol_right'] . ' to ' . $cCurrentcy['symbol_left'] . $this->show_currency(200) . $cCurrentcy['symbol_right'] . '</a></div>' .
                                '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(200) . ',' . $this->show_currency(400) .','.$precision.');>' . $cCurrentcy['symbol_left'] . $this->show_currency(200) . $cCurrentcy['symbol_right'] . ' to ' . $cCurrentcy['symbol_left'] . $this->show_currency(400) . $cCurrentcy['symbol_right'] . '</a></div>' .
                                '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(400) .',0,'.$precision.');">Over ' . $cCurrentcy['symbol_left'] . $this->show_currency(400) . $cCurrentcy['symbol_right'] . '</a></div>';
                    }

                    //ket thuc div price
                    $categories_string.='</div>';

                    $categories_string .=
 '<div style="margin-top: 5px;margin-left: 0px;"><div>' . $cCurrentcy['symbol_left'] . '<input class="input1" id="ip_price_from" value="" type="text" size="6"/>' . $cCurrentcy['symbol_right'] .
 ' to </div><div>' . $cCurrentcy['symbol_left'] . '<input class="input2" id="ip_price_to" value="" type="text" size="6">' . $cCurrentcy['symbol_right'] . '
                                <span  style="margin-right:0px; position: relative;"><a href="javascript:void(0)" onclick="search_price()" ><img src="images/go_icon.gif" width="13px" height="13px" border="0" style="position: relative; top: 4px;" /></a></span></div>
                            </div>' .
                            '<input type="hidden" name="cya" id="cya" value="" />' .
                            '<input type="hidden" name="price_to" id="price_to" value="" />' .
                            '<input type="hidden" name="price_from" id="price_from" value="" />' .
                            '<input type="hidden" name="filter_sort" id="filter_sort" value="2a"/>' .
                            '<input type="hidden" name="filter_page" id="filter_page" value="1" />';

                    //ket thuc div div_all_price
                    $categories_string.='</div>';

//-------------------------BRAND Filter DIV edit by donghp add id="manuAll----------------------------
                    $categories_string.= '<div class="more" style="margin: 10px 0px 0px 10px; width: 120px;"><span style="color:#E17009; font-weight: bold;">Brand</span>';
                    $categories_string.= '<div id="clear" style="float:left; margin: 0px 0px 0px 10px; width: 115px;"><a href="#">Clear</a></div>';
                    $categories_string.= '<div style="margin-left:0px; margin-top:5px; color: #2E6E9E;" id="show_manu">';
                    $i = 0;
                    $menu = array();
                    if (!count($this->manufactures)) {
                        $menu = $this->get_menu();
                    }
                    if (is_array($menu) && count($menu)) {
                        foreach ($menu as $value) {
                            $categories_string.= '<div id="mamu_' . $value['manufacturers_id'] . '"> <input name="manu_ids[]" id="manu_ids[]" type="checkbox" value="' . $value['manufacturers_id'] . '"/>' . $value['manufacturers_name'] . '&nbsp;' . $this->cya_count_products_in_manu($id, $value['manufacturers_id']) . '</div>';
                            $i++;
                        }
                    }

                    //ket thuc div show_manu
                    $categories_string.='</div>';
                    $categories_string.= '<div id="seemore" class="see_more"  style="margin-bottom:2px; margin-left: 20px;"><a href="#"> &raquo; See more</a></div>';
                    //ke thuc div more
                    $categories_string.='</div>';
                    //het div div_content_filter
                    $categories_string.='</div>';
                }
            } else {
                if ($tree[$counter]['level'] == 1 && !$this->flag_level_1) {
                    $categories_string.='<div id="div_content_filter" class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active div-filter-price">';
                    $this->flag_level_1 = true;
                }
                
				array_pop($cPath_array_new);
				
                if (!count($cPath_array_new) || !in_array($cparent, $cPath_array_new) || $id == $cid || in_array($cid, $cPath_array_new)) {
                    $padding_left = 2;
                    for ($i = 0; $i < $tree[$counter]['level']; $i++) {
                        //$categories_string .= "&nbsp;&nbsp;";
                        $padding_left+=5;
                    }

                    $div_w = 140 - $padding_left;
                    // start show name category
                    $categories_string .= '<a href="';

                    if ($tree[$counter]['parent'] == 0) {
                        $cPath_new = 'cPath=' . $counter;
                    } else {
                        $cPath_new = 'cPath=' . $tree[$counter]['path'];
                    }

                    $categories_string .= tep_href_link(FILENAME_DEFAULT, $cPath_new) . '">';


                    $categories_string .= '<div class="url">
                        <input type="hidden" value="' . tep_href_link(FILENAME_DEFAULT, $cPath_new) . '">
                    </div>';
                    if (isset($cPath_array) && in_array($counter, $cPath_array)) {
                        if ($id == $cid) {
                            $categories_string .= '<strong class="black_bolded">';
                        } else {
                            $categories_string .= '<strong>';
                        }
                    }
					//-------Show product count value in category- Edit by donghp --------
                    $categories_string .= '<span class="block_title border_bottom" style="margin-left: ' . $padding_left . 'px; width:' . $div_w . 'px;">' . $tree[$counter]['name'];
                    if (SHOW_COUNTS == 'true' && $tree[$counter]['level']) {
                        
						$products_in_category = tep_count_products_in_category($counter);
						
                        if ($products_in_category > 0) {
                            $categories_string .= '&nbsp;(' . $products_in_category . ')';
                        }
                    }

                    $categories_string .= '</span>
                    <div class="clear"></div>';
                    if (isset($cPath_array) && in_array($counter, $cPath_array)) {
                        $categories_string .= '</strong>';
                    }
                    $categories_string .= '</a>';
                }

                if ($tree[$counter]['next_id'] == false || !$tree[$tree[$counter]['next_id']]['level']) {
                    $categories_string .='<div style="clear: both; padding-top: 10px;"></div>';
                    $categories_string .= '</div>';
                }

               
			    if (!count($cPath_array_new) || !in_array($cparent, $cPath_array_new) || $id == $cid || in_array($cid, $cPath_array_new)) {
                    if (!$this->check_sub_cat($counter) && $id == $cid) {
						//--------By DongHP Show Price Fillter----------
                        $categories_string.='<div id="div_all_price" style="margin: 10px 0px 0px 10px; width:120px;">
                    <span style="color:#E17009; font-weight: bold;">Price </span>' .
                                '<div id="any_price" class="anyPrice" style="margin:5px 0px 0px 10px; "><a href="#" onclick="javascript:any_price_click();"> &laquo;Any price</a></div>' .
                                '<div class="price" style="margin-left:0px;">';
                      
					    //-------Hien thi thang gia de tim kiem-------------
						
                        if (SHOW_COUNTS == 'true') {
                            $categories_string .= '<div> <a href="javascript:void(0);" onclick="choose_price(0,' . $this->show_currency(40).','.$precision.');">Under ' . $cCurrentcy['symbol_left'] . $this->show_currency(40) . $cCurrentcy['symbol_right'] . '</a>&nbsp;' . $this->cya_count_products_in_price($id, 0, $this->show_currency(40)) . '</div>' .
                                    '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(40) . ',' . $this->show_currency(100) .','.$precision.');">' . $cCurrentcy['symbol_left'] . 
									$this->show_currency(40) . $cCurrentcy['symbol_right'] . ' to ' . $cCurrentcy['symbol_left'] . $this->show_currency(100) . $cCurrentcy['symbol_right'] . '</a>&nbsp;
                    ' . $this->cya_count_products_in_price($id, $this->show_currency(40), $this->show_currency(100)) . '</div>' .
                                    '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(100) . ',' . $this->show_currency(200) .','.$precision.');">' . $cCurrentcy['symbol_left'] . $this->show_currency(100) . $cCurrentcy['symbol_right'] . ' to ' . $cCurrentcy['symbol_left'] . $this->show_currency(200) . $cCurrentcy['symbol_right'] . '</a>&nbsp;
                    ' . $this->cya_count_products_in_price($id, $this->show_currency(100), $this->show_currency(200)) . '</div>' .
                                    '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(200) . ',' . $this->show_currency(400) .','.$precision.');">' . $cCurrentcy['symbol_left'] . $this->show_currency(200) . $cCurrentcy['symbol_right'] . ' to ' . $cCurrentcy['symbol_left'] . $this->show_currency(400) . $cCurrentcy['symbol_right'] . '</a>&nbsp;
                    ' . $this->cya_count_products_in_price($id, $this->show_currency(200), $this->show_currency(400)) . '</div>' .
                                    '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(400) . ',0,'.$precision.');">Over ' . $cCurrentcy['symbol_left'] . $this->show_currency(400) . $cCurrentcy['symbol_right'] . '</a>&nbsp;
                    ' . $this->cya_count_products_in_price($id, $this->show_currency(400), 9999999999) . '</div>';
                        } else {
                            $categories_string .= '<div> <a href="javascript:void(0);" onclick="choose_price(0,' . $this->show_currency(40).','.$precision.');">Under ' . $cCurrentcy['symbol_left'] . $this->show_currency(40) . $cCurrentcy['symbol_right'] . '</a></div>' .
                                    '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(40) . ',' . $this->show_currency(100) .','.$precision.');">' . $cCurrentcy['symbol_left'] . $this->show_currency(40) . $cCurrentcy['symbol_right'] . ' to ' . $cCurrentcy['symbol_left'] . $this->show_currency(100) . $cCurrentcy['symbol_right'] . '</a></div>' .
                                    '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(100) . ',' . $this->show_currency(200) .','.$precision.');">' . $cCurrentcy['symbol_left'] . $this->show_currency(100) . $cCurrentcy['symbol_right'] . ' to ' . $cCurrentcy['symbol_left'] . $this->show_currency(200) . $cCurrentcy['symbol_right'] . '</a></div>' .
                                    '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(200) . ',' . $this->show_currency(400) .','.$precision.');>' . $cCurrentcy['symbol_left'] . $this->show_currency(200) . $cCurrentcy['symbol_right'] . ' to ' . $cCurrentcy['symbol_left'] . $this->show_currency(400) . $cCurrentcy['symbol_right'] . '</a></div>' .
                                    '<div> <a href="javascript:void(0);
                    " onclick="choose_price(' . $this->show_currency(400) .',0,'.$precision.')
                    ">Over ' . $cCurrentcy['symbol_left'] . $this->show_currency(400) . $cCurrentcy['symbol_right'] . '</a></div>';
                        }

                        //ket thuc div price
                        $categories_string.='</div>';

                        $categories_string .=
                                '<div style="margin-top: 5px;margin-left: 0px;"><div>' . $cCurrentcy['symbol_left'] . '<input class="input1" id="ip_price_from" value="" type="text" size="2"/>' . $cCurrentcy['symbol_right'] . ' to </div><div>' . $cCurrentcy['symbol_left'] . '<input class="input2" id="ip_price_to" value="" type="text" size="2">' . $cCurrentcy['symbol_right'] . '
                                <span  style="margin-right:0px;
                    position: relative;
                    "><a href="javascript:void(0)" onclick="search_price()" ><img src="images/go_icon.gif" width="13px" height="13px" border="0" style="position: relative;
                    top: 4px;
                    " /></a></span></div>
                            </div>' .
                                '<input type="hidden" name="cya" id="cya" value="" />' .
                                '<input type="hidden" name="price_to" id="price_to" value="" />' .
                                '<input type="hidden" name="price_from" id="price_from" value="" />' .
                                '<input type="hidden" name="filter_sort" id="filter_sort" value="2a"/>' .
                                '<input type="hidden" name="filter_page" id="filter_page" value="1" />';

                        //ket thuc div div_all_price
                        $categories_string.='</div>';
					//---------------------BRAND Filter DIV ---------------------------------------
                        $categories_string.= '<div class="more" style="margin: 10px 0px 0px 10px;
                    width: 120px;"><span style="color:#E17009; font-weight: bold;">Brand D</span>';
                        $categories_string.= '<div id="clear" style="float:left; margin: 0px 0px 0px 10px; width: 115px;"><a href="#">Clear</a></div>';
                        $categories_string.= '<div style="margin-left:0px; margin-top:5px; color: #2E6E9E;" id="show_manu">';
                        $i = 0;
                        $menu = array();
                        if (!count($this->manufactures)) {
                            $menu = $this->get_menu();
                        }
						
                        if (is_array($menu) && count($menu)) {
                            foreach ($menu as $value) {
								
 $c_d=$this->cya_count_products_in_manu($id, $value['manufacturers_id']);
 								
                                $categories_string.= '<div id="mamu_' . $value['manufacturers_id'] . '"> <input name="manu_ids[]" id="manu_ids[]" type="checkbox" value="' . $value['manufacturers_id'] . '"/>' . $value['manufacturers_name'] . '&nbsp;' . $c_d. '</div>';
                                $i++;
                            }
                        }

                        //ket thuc div show_manu
                        $categories_string.='</div>';
                        $categories_string.= '<div id="seemore" class="see_more"  style="margin-bottom:2px; margin-left: 20px;"><a href="#"> &raquo; See more</a></div>';
                        //ke thuc div more
                        $categories_string.='</div>';
                    }
                }
            }
        } else {
            //cho hien ra binh thuong

            $categories_string .= '<h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-corner-all"><div><span class="ui-icon ui-icon-plus" style="float: left;"></span>';

            // start show name category
            $categories_string .= '<a href="';

            if ($tree[$counter]['parent'] == 0) {
                $cPath_new = 'cPath=' . $counter;
            } else {
                $cPath_new = 'cPath=' . $tree[$counter]['path'];
            }

            $categories_string .= tep_href_link(FILENAME_DEFAULT, $cPath_new) . '">';

            if (isset($cPath_array) && in_array($counter, $cPath_array)) {
                $categories_string .= '<strong>';
            }

            // display category name
            $categories_string .= $tree[$counter]['name'];

            if (isset($cPath_array) && in_array($counter, $cPath_array)) {
                $categories_string .= '</strong>';
            }

            if (tep_has_category_subcategories($counter)) {
                //$categories_string .= '-&gt;';
            }

            $categories_string .= '</a>';

            if (SHOW_COUNTS == 'true') {
                $products_in_category = tep_count_products_in_category($counter);
                if ($products_in_category > 0 && $tree[$counter]['level']) {
                    $categories_string .= '&nbsp;(' . $products_in_category . ')';
                }
            }
            //end show name category

            $categories_string .= '</div></h3>';
        }

        if ($tree[$counter]['next_id'] != false) {
            $this->tep_show_category($tree[$counter]['next_id']);
        }
    }

    function getData() {
        global $categories_string, $tree, $languages_id, $cPath, $cPath_array, $currency, $currencies;
        
		   //edit by donghp set precision
		$currencies_arr = $currencies->currencies;
        
		$cCur = $currencies_arr[$currency]; //$cCurrentcy['decimal_places']
				
        $precision = $cCur['decimal_places']; 

	   //============================================================
	   
        $categories_string = '';
        $categories_string_top = '';
        $tree = array();

        $categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '0' and c.categories_id = cd.categories_id and cd.language_id='" . (int) $languages_id . "' order by sort_order, cd.categories_name");
        while ($categories = tep_db_fetch_array($categories_query)) {
            $tree[$categories['categories_id']] = array('name' => $categories['categories_name'],
                'parent' => $categories['parent_id'],
                'level' => 0,
                'path' => $categories['categories_id'],
                'next_id' => false);

            if (isset($parent_id)) {
                $tree[$parent_id]['next_id'] = $categories['categories_id'];
            }

            $parent_id = $categories['categories_id'];

            if (!isset($first_element)) {
                $first_element = $categories['categories_id'];
            }
        }

        if (tep_not_null($cPath)) {
            $new_path = '';
            reset($cPath_array);
            while (list($key, $value) = each($cPath_array)) {
                unset($parent_id);
                unset($first_id);
                $categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '" . (int) $value . "' and c.categories_id = cd.categories_id and cd.language_id='" . (int) $languages_id . "' order by sort_order, cd.categories_name");
                if (tep_db_num_rows($categories_query)) {
                    $new_path .= $value;
                    while ($row = tep_db_fetch_array($categories_query)) {
                        $tree[$row['categories_id']] = array('name' => $row['categories_name'],
                            'parent' => $row['parent_id'],
                            'level' => $key + 1,
                            'path' => $new_path . '_' . $row['categories_id'],
                            'next_id' => false);

                        if (isset($parent_id)) {
                            $tree[$parent_id]['next_id'] = $row['categories_id'];
                        }

                        $parent_id = $row['categories_id'];

                        if (!isset($first_id)) {
                            $first_id = $row['categories_id'];
                        }

                        $last_id = $row['categories_id'];
                    }
                    $tree[$last_id]['next_id'] = $tree[$value]['next_id'];
                    $tree[$value]['next_id'] = $first_id;
                    $new_path .= '_';
                } else {
                    break;
                }
            }
        }
	
        $this->tep_show_category($first_element);
        $str_jquery = '<script type="text/javascript">';    
		                    
/*		$str_jquery.= 'function IsEmpty(StringToCheck)
			{
			StrToCheck= StrToCheck.replace(/^\s+|\s+$/, "");						
			if( StrToCheck.length==0)
			return false;
			else
			return true;
			}';   
	*/		
     $str_jquery.= '$(document).ready(function()
                        { 
                            $("h3").hover(function(e) {
                                $(this).addClass("ui-state-hover");  
                            }, function() {
                                $(this).removeClass("ui-state-hover");  
                            });
                            var h3_content_filter_top = $("h3#h3_content_filter").html();  
                            var div_content_filter_top = $("div#div_content_filter").html();  
                            
                            if ( h3_content_filter_top != null){
                                $("h3#h3_content_filter_top").html(h3_content_filter_top);  
                                $("div#div_content_filter_top").html(div_content_filter_top);  
                                $("h3#h3_content_filter_top").removeClass("deactive");
                                $("div#div_content_filter_top").removeClass("deactive");

                                $("h3#h3_content_filter").remove();  
                                $("div#div_content_filter").remove(); 
                            }else{
                                $("h3#h3_content_filter_top").hide();  
                                $("div#div_content_filter_top").hide();
                            }
                            var i=0;
                            var content = $(".more input").length;
                            $(".more div").each(function(index){
                                if(index>21){
                                    $(this).addClass("deactive");
                                    $(".see_more").removeClass("see_more");
                                    $("div#seemore").removeClass("deactive");
                                }
                            });

                            $("#seemore").click(function(){
                                $(".more div").removeClass("deactive");
                                $(this).addClass("see_more");
                                return false;
                            });
                            
                            $("div#show_manu input:checkbox").click(function(){
                                $("input#filter_sort").val("2a");
                                $("input#filter_page").val(1);  
                                
                                filter_product2(1);//click checkbox
                            });

                            $("#clear").click(function(){
								                    
                                $.each($("div.font_bold"), function() { 
                                    $(this).removeClass("font_bold");
                                });
                                $("input#filter_sort").val("2a");
                                $("input#filter_page").val(1);
                                $(this).hide();
                                $("input:checkbox").removeAttr("checked");
                                $(".contentText").empty();
                                filter_product();
                                return false;

                            });
                            
                            $("#any_price").click(function(){
                                $("input#filter_sort").val("2a");
                                $("input#filter_page").val(1);
                                $(".price div").removeClass("a12");
                                $(".price div").show();
                                $(this).hide();
                                $(".input1").attr("value", "");
                                $(".input2").attr("value", "");
                                $("input#price_from").attr("value", "");
                                $("input#price_to").attr("value", "");
                                filter_product2();
                                return false;
                            });
                            
                        });
                        
						function any_price_click()
						{
								$("input#filter_sort").val("2a");
                                $("input#filter_page").val(1);
                                $(".price div").removeClass("a12");
                                $(".price div").show();
                                $(this).hide();
                                $(".input1").attr("value", "");
                                $(".input2").attr("value", "");
                                $("input#price_from").attr("value", "");
                                $("input#price_to").attr("value", "");
                                
								filter_product2();//quay lai trang thai ban dau

							}
                        // function choose price - edit by donghp
                        function choose_price(priceF, priceT,precision){
							
                            $("input#filter_sort").val("2a");
                            $("input#filter_page").val(1);
                            $(".price div").not(".a12").hide();
                            $(".anyPrice").show();
                            if(priceF>0){
								pF= parseFloat(priceF).toFixed(precision); // parse as float number
                                $(".input1").attr("value", pF);
                                $("input#price_from").attr("value", pF);
                            }else{
                                $(".input1").attr("value", 0);
                                $("input#price_from").attr("value", 0);
                            }
							
                            if(priceT>0){
								pT= parseFloat(priceT).toFixed(precision); // parse as float number															
                                $(".input2").attr("value", pT);
                                $("input#price_to").attr("value", pT);
                            }else{
                                $(".input2").attr("value", "");
                                $("input#price_to").attr("value", "");
                            }
							
                            filter_product2(2);//se ko thay doi menu price
                            return false;
                        }
                        // get all manufacturies_id
                        function get_checked_manu(){
                             var allVals = ""; 
                             $.each($("div.font_bold"), function() {
                                $(this).removeClass("font_bold");
                             });
                             //outputs: 1:Red 2:Orange 3:Green
                             $("input[name=\'manu_ids[]\']:checkbox:checked").each(function() {
                                $("#clear").show();
                                $("div#mamu_"+$(this).val()).addClass("font_bold");
                                allVals = allVals+"_"+$(this).val();                             
                             });
                             if(allVals!=""){
                                allVals = allVals.substring(1);
                             }
                             return allVals;
                        }
                        
                        // action click go
                        function search_price(){
                            var price_from = $("input#ip_price_from").val();
                            var price_to = $("input#ip_price_to").val();
                            if(price_from|| price_to){
                                $("input#filter_sort").val("2a");
                                $("input#filter_page").val(1);
                                $("input#price_from").attr("value", price_from);
                                $("input#price_to").attr("value", price_to); 
                                $(".price div").not(".a12").hide();
                                $(".anyPrice").show();
                                filter_product2();//edit by donghp
                            }                            
                        }
                        
						function IsEmpty(StringToCheck)
						{
							StrToCheck= StrToCheck.replace(/^\s+|\s+$/, "");						
							if( StrToCheck.length==0)
							return false;
							else
							return true;
						}
			
                        // post and filter product
                        function filter_product(){
                            $(".contentText").html("<img src=\'images/loading29.gif\' border=\'0\'>");
                            $("form[name=\'filter\']").addClass("deactive");
                            var cid = $("input#cid").val();
                            var cPath = $("input#cPath").val();
                            var price_from = $("input#price_from").val();
                            var price_to = $("input#price_to").val();
                            var manu_ids = get_checked_manu();
                            var sort = $("input#filter_sort").val();
                            var page = $("input#filter_page").val();   
                          $.post("filter.php?categories_id="+cid+"&price_from="+price_from+"&price_to="+price_to+"&manufacturers_id="+manu_ids+"&sort="+sort+"&page="+page+"&cPath="+cPath,
                                function(data) {
                                    $(".contentText").html(data);
                            });					
                        }
						
function filter_product2(C){ //ad by donghp into column left

  $(".contentText").html("<img src=\'images/loading29.gif\' border=\'0\'>");
  $("form[name=\'filter\']").addClass("deactive");
  var cid = $("input#cid").val();
  var cPath = $("input#cPath").val();
  var price_from = $("input#price_from").val();
  var price_to = $("input#price_to").val();
  var manu_ids = get_checked_manu();
  var sort = $("input#filter_sort").val();
  var page = $("input#filter_page").val();  
							
if (typeof(C) === "undefined") {  C=0; }


$.post("filter.php?categories_id="+cid+"&price_from="+price_from+"&price_to="+price_to+"&manufacturers_id="+manu_ids+"&sort="+sort+"&page="+page+"&cPath="+cPath,
                                function(data) {                                    
									//var arr = data.split("[:|:]");
								$(".contentText").html(data);
									//$(".more").html(arr[1]);									
                            });		

// neu c = 0 va price !="" dang chon gia thi khi chon them manu_ids se ko hien thi lai menu price															
if (manu_ids!="")
  {	  
  alert(C);
  
  ok = (price_from != "") || (price_to != "");
  
  if (C == 2 || (C==0 && ok)) //se ko hien thi thang gia khi ma co price
     {		     $.post("brandcat_left.php?categories_id="+cid+"&price_from="+price_from+"&price_to="+price_to+"&manufacturers_id="+manu_ids+"&sort="+sort+"&page="+page+"&cPath="+cPath+"&case="+C,
			function(data) { 
			    $(".more").html(data); 	
		});

	 }
  else
    {    $.post("brand_cat.php?categories_id="+cid+"&price_from="+price_from+"&price_to="+price_to+"&manufacturers_id="+manu_ids+"&sort="+sort+"&page="+page+"&cPath="+cPath+"&case="+C,
			function(data) { 
 			    var arr = data.split("[:|:]");                               
			    $(".more").html(arr[1]); 	
				$("#div_all_price").html(arr[0]); 																	
		});
	}
	
  } //end if manu_ids
else
   {	 alert("oooooooooo");   $.post("brand_cat.php?categories_id="+cid+"&price_from="+price_from+"&price_to="+price_to+"&manufacturers_id="+manu_ids+"&sort="+sort+"&page="+page+"&cPath="+cPath,
			function(data) { 
			    $(".more").html(data); 																		
		});			
   }

}//end function 

function filter_product3(){ //ad by donghp into column left

alert("product 3");
                            $(".contentText").html("<img src=\'images/loading29.gif\' border=\'0\'>");
                            $("form[name=\'filter\']").addClass("deactive");
                            var cid = $("input#cid").val();
                            var cPath = $("input#cPath").val();
                            var price_from = "";
                            var price_to = "";
                            var manu_ids = get_checked_manu();
                            var sort = $("input#filter_sort").val();
                            var page = $("input#filter_page").val();  

							 
$.post("filter.php?categories_id="+cid+"&price_from="+price_from+"&price_to="+price_to+"&manufacturers_id="+manu_ids+"&sort="+sort+"&page="+page+"&cPath="+cPath,
                                function(data) {                                    								
								$(".contentText").html(data);
									
                            });					
	

$.post("brandcat_left.php?categories_id="+cid+"&price_from="+price_from+"&price_to="+price_to+"&manufacturers_id="+manu_ids+"&sort="+sort+"&page="+page+"&cPath="+cPath,
											function(data) {                                
												 $(".more").html(data); 																	
										});	
																							
}						
//=====================================================================================================
						function change_sort(sort_by){
                            var cur_sort = $("input#filter_sort").val();
                            var fsort = parseInt(cur_sort.substring(0, 1));
                            var lsort = cur_sort.substring(1, 2);
                            
                            if(sort_by==fsort){
                                if(lsort=="a"){
                                    $("input#filter_sort").val(sort_by+"d");
                                }else{
                                    $("input#filter_sort").val(sort_by+"a");
                                }
                            }else{
                                $("input#filter_sort").val(sort_by+"a");
                            }
                            $("input#filter_page").val(1);
                            filter_product();
                        }
                        
                        function change_page(page_new){
                            $("input#filter_page").val(page_new);
                            filter_product();
                        }
                        


                        var compare_text = "' . IMAGE_BUTTON_COMPARE . '";
                        var products_text = "' . IMAGE_BUTTON_COMPARE_PRODUCTS . '";
                        function validateButton() {                        
                            var getVal = $("input[name=\'checkgroup[]\']:checkbox:checked").length;
                            var compare = [];
                            var cookie_compare = $.cookie(\'compare\');
                            if (cookie_compare) {
                                compare = cookie_compare.split(\',\');
                            }
                                getVal = compare.length;

                                        if (getVal <  2)
                                        {
                                                alert("' . TEXT_AT_LEAST_N_PRODUCTS . '");
                                                return false;		      
                                        }
                                        if (getVal >  4)
                                        {
                                        alert("' . TEXT_COMPARE_MAX_N_PRODUCTS . '");
                                        return false;		
                                }
                        }
                        if (!Array.prototype.indexOf)
                        {
                          Array.prototype.indexOf = function(elt /*, from*/)
                          {
                            var len = this.length;

                            var from = Number(arguments[1]) || 0;
                            from = (from < 0)
                                 ? Math.ceil(from)
                                 : Math.floor(from);
                            if (from < 0)
                              from += len;

                            for (; from < len; from++)
                            {
                              if (from in this &&
                                  this[from] === elt)
                                return from;
                            }
                            return -1;
                          };
                        }

                        function updateCompare() {
                                var compare = [];
                                var cookie_compare = $.cookie(\'compare\');
                                if (cookie_compare) {
                                        compare = cookie_compare.split(\',\');
                                }                                
                                $("input[name=\'checkgroup[]\']:checkbox").each(function () {
                                        if (this.checked) {
                                                if (compare.indexOf(this.value) == -1) {
                                                        compare.push(this.value);
                                                }
                                        } else {
                                                if (compare.indexOf(this.value) > -1) {
                                                        compare[compare.indexOf(this.value)] = null;
                                                }
                                        }
                                });
                                cookie_compare = compare.join(\',\');
                                cookie_compare = cookie_compare.replace(/^([,]+)/, \'\'); 
                                cookie_compare = cookie_compare.replace(/([,]{2,})/, \',\'); 
                                cookie_compare = cookie_compare.replace(/([,]+)$/, \'\'); 

                                $.cookie(\'compare\', cookie_compare);
                                updateCompareButton();
                        }
                        function updateCompareButton() {
                            var compare = [];
                                var cookie_compare = $.cookie(\'compare\');
                                if (cookie_compare) {
                                        compare = cookie_compare.split(\',\');
                                }
                                if (compare.length > 0) {
                                        $("#compare_button_clear").find(":button").button({disabled: false});
                                } else {
                                        $("#compare_button_clear").find(":button").button({disabled: true});
                                }
                            $("#compare_button").find(":button").button(\'option\', \'label\', compare_text + \' \' + compare.length + \' \' + products_text);
                        }
                        function clearCompareCheckboxes() {
                                $.cookie(\'compare\', \'\');
                                setCompareCheckboxes();
                        }
                        function setCompareCheckboxes() {
                            var compare = [];
                            var cookie_compare = $.cookie(\'compare\');
                            if (cookie_compare) {
                                compare = cookie_compare.split(\',\');
                            }

                            $("input[name=\'checkgroup[]\']:checkbox").each(function () {
                                if (compare.indexOf(this.value) != -1) {
                                    this.checked = true;
                                } else {
                                    this.checked = false;
                                        };
                            });
                                updateCompareButton();
                        }
                        $().ready(function () {
                                setCompareCheckboxes();
                                $("#compare_button_clear").click(function() {clearCompareCheckboxes();return false;});
                        });

                        </script>';

        $str_css = '<style type="text/css"/>
                         .deactive{
                            display: none;
                         }
                         .see_more{
                            display: none;
                         }                         
                         .anyPrice{
                            display: none;
                         }
                         #clear{
                                 display: none;
                             }

                     </style>';
        $this->jquery_css .= $str_jquery . $str_css;
        $data = $this->jquery_css;
        $data .= '<div id="d_put_content_top_cat" style="margin-bottom: 5px;"></div><div class="clear"></div>';
        $data .= '<div id="adminAppMenu" class="ui-accordion ui-widget ui-helper-reset ui-accordion-icons">';
        $data .= '<h3 id="h3_content_filter_top" class="ui-accordion-header ui-helper-reset ui-state-default ui-state-active ui-corner-top deactive"></h3>';
        $data .= '<div id="div_content_filter_top" class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active div-filter-price deactive"></div>';
        $data .= $categories_string;
        $data .= '</div>';
        $data.= ' <input type="hidden" name="cid" id="cid" value="' . $cPath_array[count($cPath_array) - 1] . '"/>';
        $data.= ' <input type="hidden" name="cPath" id="cPath" value="' . $cPath . '"/>';


        return $data;
    }

    function execute() {
        global $SID, $oscTemplate;

        if ((USE_CACHE == 'true') && empty($SID)) {
            $output = tep_cache_categories_box();
        } else {
            $output = $this->getData();
        }

        $oscTemplate->addBlock($output, $this->group);
    }

    function isEnabled() {
        return $this->enabled;
    }

    function check() {
        return defined('MODULE_BOXES_CATEGORIES_CYA');
    }

    function install() {
        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Search Module', 'MODULE_BOXES_CATEGORIES_CYA', 'True', 'Do you want to add the module to your shop?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Content Placement', 'MODULE_BOXES_CATEGORIES_CONTENT_PLACEMENT_CYA', 'Left Column', 'Should the module be loaded in the left or right column?', '6', '1', 'tep_cfg_select_option(array(\'Left Column\', \'Right Column\'), ', now())");
        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_BOXES_CATEGORIES_SORT_ORDER_CYA', '0', 'Sort order of display. Lowest is displayed first.', '6', '0', now())");
    }

    function remove() {
        tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
        return array('MODULE_BOXES_CATEGORIES_CYA', 'MODULE_BOXES_CATEGORIES_CONTENT_PLACEMENT_CYA', 'MODULE_BOXES_CATEGORIES_SORT_ORDER_CYA');
    }

}

?>
